package com.app.xinfadi.ui.purchase_price;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.app.xinfadi.R;
import com.app.xinfadi.base.BaseFragment;
import com.app.xinfadi.dao.HomeDao;
import com.app.xinfadi.dao_bean.CenterPurchasePlanListBean;
import com.app.xinfadi.dao_bean.bean.CenterPurchasePlanBean;
import com.app.xinfadi.dao_bean.bean.refresh_bean.center_role.CenterRoleExamineAndApproveListRefreshBean;
import com.app.xinfadi.dao_bean.bean.refresh_bean.lead_role.LeadRoleRefreshBean;
import com.app.xinfadi.ui.purchase_plan.view.CenterPurchasePlanListAdapter;
import com.app.xinfadi.util.LogUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * function: 报价单列表
 * Created by Wutengfei on 2016/1/26.
 */
public class PurchasePriceFragment extends BaseFragment {
    private static final java.lang.String TAG = PurchasePriceFragment.class.getSimpleName();
    private View view;
    private PullToRefreshListView pullToRefreshListView;
    private CenterPurchasePlanListAdapter purchasePriceListAdapter;
    private int type;
    private String status;
    private int currentPage = 1;
    private String center_status;

    /**
     * @param type
     * @param status
     * @param center_status 中心类别 0 审核列表 1 中心新增列表
     * @return
     */
    public static PurchasePriceFragment getInstance(int type, String status, String center_status) {
        PurchasePriceFragment instance = new PurchasePriceFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        args.putString("status", status);
        args.putString("center_status", center_status);
        instance.setArguments(args);
        return instance;
    }

    /**
     * @param type   类型 0 采购员角色 1 中心角色 2 领导角色
     * @param status 状态:<br/>
     *               -1 无 采购员
     *               中心：
     *               待审：01
     *               已审批：21
     *               领导：
     *               待处理：11
     *               执行中：21
     *               已完成：03
     * @return
     */
    public static PurchasePriceFragment getInstance(int type, String status) {
        PurchasePriceFragment instance = new PurchasePriceFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        args.putString("status", status);
        instance.setArguments(args);
        return instance;
    }

    /**
     * @param type 类型 0 采购员角色 1 中心角色
     * @return
     */
    public static PurchasePriceFragment getInstance(int type) {
        PurchasePriceFragment instance = new PurchasePriceFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        instance.setArguments(args);
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.pulltorefresh_listview_fragment, null);
        getBundle();
        initView();
        initData();
        EventBus.getDefault().register(this);
        return view;
    }

    private void getBundle() {
        type = getArguments().getInt("type", 0);
        status = getArguments().getString("status");
        if (TextUtils.isEmpty(status)) {
            status = "-1";
        }
        center_status = getArguments().getString("center_status");
    }

    /**
     * 中心角色 审核列表刷新
     *
     * @param centerRoleExamineAndApproveListRefreshBean
     */
    public void onEventMainThread(CenterRoleExamineAndApproveListRefreshBean centerRoleExamineAndApproveListRefreshBean) {
        if (centerRoleExamineAndApproveListRefreshBean.refresh) {
            currentPage = 1;
            getPlans();
        }
    }

    /**
     * 领导角色 界面刷新
     *
     * @param leadRoleRefreshBean
     */
    public void onEventMainThread(LeadRoleRefreshBean leadRoleRefreshBean) {
        if (leadRoleRefreshBean.refresh) {
            currentPage = 1;
            getPlans();
        }
    }

    private void initView() {
        initListView();
    }

    private void initListView() {
        pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.pullToRefreshListView);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                String label = DateUtils.formatDateTime(context, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
                        | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                if (PullToRefreshBase.Mode.PULL_FROM_START == pullToRefreshListView.getCurrentMode()) {
//                    Log.e("pull_down", "up");
//                    pageNo = 1;
//                    taskAdapter.getData().clear();
//                    initListViewData();
                    currentPage = 1;
                    getPlans();
                    new GetDataTask().execute();
                } else if (PullToRefreshBase.Mode.PULL_FROM_END == pullToRefreshListView.getCurrentMode()) {
//                    Log.e("pull_down", "down");
                    currentPage++;
                    getPlans();
                    new GetDataTask().execute();
                }
//                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
            }
        });
        if (!TextUtils.isEmpty(center_status)) {
            purchasePriceListAdapter = new CenterPurchasePlanListAdapter(context, center_status);
        } else
            purchasePriceListAdapter = new CenterPurchasePlanListAdapter(context);
        pullToRefreshListView.setAdapter(purchasePriceListAdapter);

        getPlans();
        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LogUtil.e(TAG, "position:" + position);
                Intent intent = new Intent(context, PurchasePriceDetailsActivity.class);
                intent.putExtra("type", type);
                intent.putExtra("status", status);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", purchasePriceListAdapter.getData().get(position - 1));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void initData() {

    }

    public void getPlans() {

//        String myPatientsRingMessage[] = {"黄瓜", "西红柿", "青椒", "土豆", "油", "白菜", "胡萝卜", "大豆", "海带", "香菜"};
//        List<CenterPurchasePlanBean> purchasePlanBeanList = new ArrayList<CenterPurchasePlanBean>();
//        // 已经选的 49
//        for (int i = 0; i < myPatientsRingMessage.length * 1; i++) {
//            CenterPurchasePlanBean purchasePlanBean = new CenterPurchasePlanBean();
//            purchasePlanBean.NAME = myPatientsRingMessage[i % 10];
//            purchasePlanBeanList.add(purchasePlanBean);
//        }
//        refreshAdapter(purchasePlanBeanList);
        if (type == 1) {
            HomeDao homeDao = new HomeDao();
            homeDao.getCenterPlans(currentPage, status, type + "", new Response.Listener<CenterPurchasePlanListBean>() {
                @Override
                public void onResponse(CenterPurchasePlanListBean centerPurchasePlanListBean) {
                    if (centerPurchasePlanListBean.success) {
                        if (currentPage == 1) {
                            purchasePriceListAdapter.getData().clear();
                            purchasePriceListAdapter.notifyDataSetChanged();
                        }
                        if (centerPurchasePlanListBean.plans != null && centerPurchasePlanListBean.plans.size() > 0) {
                            refreshAdapter(centerPurchasePlanListBean.plans);
                        }
                    }

                }
            });
        } else if (type == 2) {
            HomeDao homeDao = new HomeDao();
            homeDao.getAdminPlans(currentPage, status, type + "", new Response.Listener<CenterPurchasePlanListBean>() {
                @Override
                public void onResponse(CenterPurchasePlanListBean centerPurchasePlanListBean) {
                    if (centerPurchasePlanListBean.success) {
                        if (currentPage == 1) {
                            purchasePriceListAdapter.getData().clear();
                            purchasePriceListAdapter.notifyDataSetChanged();
                        }
                        if (centerPurchasePlanListBean.plans != null && centerPurchasePlanListBean.plans.size() > 0) {
                            refreshAdapter(centerPurchasePlanListBean.plans);
                        }
                    }
                }
            });
        }
    }

    private class GetDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            pullToRefreshListView.onRefreshComplete();
            super.onPostExecute(result);
        }
    }

    private void refreshAdapter(List<CenterPurchasePlanBean> purchasePlanBeanList) {
        purchasePriceListAdapter.getData().addAll(purchasePlanBeanList);
        purchasePriceListAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}


